package com.test.service;

import com.test.dao.*;
import com.test.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZZF on 2017/7/6.
 */
@Service
@Transactional
public class WriteDataService {

    @Autowired
    IBuildDao buildDao;
    @Autowired
    IBuildValveCurrentDao buildValveCurrentDao;
    @Autowired
    IBuildValueCurrentService buildValueCurrentService;
    @Autowired
    IBuildMeterCurrentDao buildMeterCurrentDao;
    @Autowired
    IHouseService houseService;
    @Autowired
    IBuildMeterCurrentService buildMeterCurrentService;
    @Autowired
    IBuildMeterDao buildMeterDao;

    @Autowired
    IBuildValveHistoricalService buildValveHistoricalService;

    @Autowired
    ITemperaturePanelCurrentService temperaturePanelCurrentService;

    @Autowired
    IBuildValveDao buildValveDao;


    public void writeDate() {

        //已楼栋数据做为根本
        List<Build> buildAll = buildDao.getAll(Build.class);

        //生成楼阀的实时
        buildValueCurrentService.updateOpen();

        //判断是否为空
        if (!buildAll.isEmpty()) {
            for (Build build : buildAll) {
                //查找楼的对应楼阀
                List<BuildMeter> buildMeter = buildDao.findBuildMeter(build.getUniqueId());

                List<BuildValve> buildValve = buildDao.findBuildValve(build.getUniqueId());


                boolean isBuildValve = true;

//                boolean isBuildMeter = true;


                //遍历楼阀，把楼阀的单元id为空和有值的的分隔开
                if (!buildValve.isEmpty()) {

                    List<BuildValve> valveIdList = new ArrayList();

                    //假定楼阀的单元号是有值的
                    boolean isUnitId = true;

                    for (BuildValve valve : buildValve) {

                        if (valve.getUnit() != null) {
                            valveIdList.add(valve);
                        } else {
                            valveIdList = new ArrayList<>();
                            valveIdList.add(valve);
                            isUnitId = false;
                            break;
                        }

                    }


                    for (BuildValve valveTemp : valveIdList) {

                        //假定楼阀一开始就是开的，设值为true
                        boolean buildFlag = true;

                        //获得楼阀的实时数据
                        BuildValveCurrentData valveCurrentData = buildValveCurrentDao.get(BuildValveCurrentData.class, valveTemp.getUniqueId());


                        BigDecimal actualOpening = valveCurrentData.getActualOpening();
                        if (actualOpening.intValue() == 0) {
                            buildFlag = false;
                        }

                        //添加楼阀的历史记录
                        buildValveHistoricalService.addHistorical(valveCurrentData);

                        List<House> houses;

                        BuildMeterCurrentData buildMeterCurrent;

                        if (isUnitId) {

                            //用单元号关联的户数据
                            houses = buildValveDao.findHouses(valveTemp.getUnit().getUniqueId());
                            buildMeterCurrent = buildMeterCurrentDao.
                                    findBuildMeterCurrent(valveTemp.getUnit().getUniqueId(),0);

                        } else {

                            //用楼栋号所关联的户信息
                            houses = buildDao.findHouse(valveTemp.getBuild().getUniqueId());

                          buildMeterCurrent = buildMeterCurrentDao.
                                    findBuildMeterCurrent(0,valveTemp.getBuild().getUniqueId());
                        }

                        //用单元号关联求出楼表的实时数据
                        //用楼表的楼栋id，加上判断条件unitdid  用楼阀数据来因为相关的楼表实时数据


                        houseService.simulationHouse(houses, buildFlag);

                        if (buildMeterCurrent!=null){
                            //完成楼表的模拟
                            buildMeterCurrentService.updateBuildMeter(houses, buildMeterCurrent);
                        }

                    }

                } else {
                    isBuildValve = false;

                }


                //当楼阀为空，楼表不为空时，进行楼表和户表的模拟
                if (!buildMeter.isEmpty() && isBuildValve == false) {

                    List<BuildMeter> meterList = new ArrayList();

                    //假定楼表的单元号是有值的
                    boolean isUnitId = true;

                    //把楼表里面单元号为空的挑选出来
                    for (BuildMeter meter : buildMeter) {

                        if (meter.getUnit() != null) {
                            meterList.add(meter);
                        } else {
                            meterList = new ArrayList();
                            meterList.add(meter);
                            isUnitId = false;
                            break;
                        }

                    }

                    //进行已楼表为媒介的模拟
                    for (BuildMeter metertemp : meterList) {

                        List<House> houses;


                        if (isUnitId) {
                            //单元号不为空就用单元号求的楼表关联的户数据
                            houses = buildMeterDao.findHouses(metertemp.getUnit().getUniqueId());
                        } else {

                            //单元号为空就用楼栋号求的楼表关联的户数据
                            houses = buildDao.findHouse(metertemp.getBuild().getUniqueId());
                        }

                        //进行模拟
                        houseService.simulationHouse(houses, true);

                        //楼表对象关联的楼表实时数据对象
                        BuildMeterCurrentData buildMeterCurrent = buildMeterCurrentDao.
                                get(BuildMeterCurrentData.class, metertemp.getUniqueId());

                        //完成楼表的模拟
                        buildMeterCurrentService.updateBuildMeter(houses, buildMeterCurrent);

                    }


                }

                //当楼表和楼阀都为空时，只用模拟楼栋下面关联的户数据就行了
                if (isBuildValve == false && buildMeter.isEmpty() ) {

                    List<House> houses = buildDao.findHouse(build.getUniqueId());
                    houseService.simulationHouse(houses, true);

                }


            }

            //温度面板的数据模拟
            temperaturePanelCurrentService.updatePanel();

        }


    }

}
